<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<h1>
    0
</h1>
<button>按我加1</button>
<button id="btn">按我减1</button>
<script>
    //根据标签获取文档对象
    var elH1 = document.getElementsByTagName("h1")[0];
    var elBtn = document.getElementsByTagName("button")[0];
    var elBtn2 = document.getElementById("btn");
    //定义变量
    var num = 0;
    //绑定点击事件1
    elBtn.onclick = function () {
        //+1
        num++;
        //修改标签对象的值
        elH1.innerHTML = num;
    }
    //绑定点击事件2
    elBtn2.onclick = function () {
        //-1
        num--;
        elH1.innerHTML = num;
        //控制台打印日志
        console.log(num)
    }
</script>
</body>
</html>